package kr.hkit.summary_java.std.serviceui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.JobAttributes;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.std.PrnStudent;
import kr.hkit.summary_java.std.StdMain;
import kr.hkit.summary_java.std.dao.StudentDao;
import kr.hkit.summary_java.std.dto.Student;

public class StudentUI extends JPanel implements ActionListener {
	private InputPanel pStdNum;
	private JPanel panelBtn;
	private InputPanel pStdName;
	private InputPanel pKor;
	private InputPanel pEng;
	private InputPanel pMath;
	private JLabel label;
	private JLabel lblNewLabel;
	private JButton btnAdd;
	private JButton btnPrn;
	private JButton btnAllDel;
	private JPanel panel;

	private StudentDao dao = new StudentDao();
	private JPanel prnPanel;
	
	/**
	 * Create the panel.
	 */
	public StudentUI() {

		initialize();
	}

	private void initialize() {

		panelBtn = new JPanel();
		setLayout(new BorderLayout(0, 0));

		prnPanel = new JPanel();
		add(prnPanel, BorderLayout.NORTH);

		panel = new JPanel();
		add(panel);
		panel.setLayout(new GridLayout(4, 2, 0, 0));

		pStdNum = new InputPanel("학번");
		pStdNum.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent arg0) {
				super.focusLost(arg0);

				Student std = null;

				try {
					std = dao.searchItem(new Student(Integer.parseInt(pStdNum
							.getTf().getText().trim())));
					if (std != null) {
						pStdName.setTf(std.getStdName());
						pStdName.getTf().setEditable(false);
					} else {
						JOptionPane.showMessageDialog(StudentUI.this,
								"없는 학생 정보");
						pKor.getTf().setEditable(false);
						pEng.getTf().setEditable(false);
						pMath.getTf().setEditable(false);
					}
				} catch (NumberFormatException | SQLException e) {
				}
			}

		});
		panel.add(pStdNum);

		pStdName = new InputPanel("성명");
		panel.add(pStdName);

		pKor = new InputPanel("국어점수");
		panel.add(pKor);

		label = new JLabel(" ");
		panel.add(label);

		pEng = new InputPanel("영어점수");
		panel.add(pEng);

		lblNewLabel = new JLabel(" ");
		panel.add(lblNewLabel);

		pMath = new InputPanel("수학점수");
		panel.add(pMath);
		add(panelBtn, BorderLayout.SOUTH);

		btnAdd = new JButton("입력");
		btnAdd.addActionListener(this);
		panelBtn.add(btnAdd);

		btnPrn = new JButton("출력");
		btnPrn.addActionListener(this);
		panelBtn.add(btnPrn);

		btnAllDel = new JButton("전체삭제");
		btnAllDel.addActionListener(this);
		panelBtn.add(btnAllDel);
		
		prnPanel = new PrnStudent();
		add(prnPanel, BorderLayout.NORTH);
		prnPanel.setVisible(false);
	}

	public void actionPerformed(ActionEvent e) {
		try {
			if (e.getSource() == btnAllDel) {
				btnAllDelActionPerformed(e);
			}
			if (e.getSource() == btnPrn) {
				btnPrnActionPerformed(e);
			}
			if (e.getSource() == btnAdd) {
				btnAddActionPerformed(e);
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		revalidate();
	}

	protected void btnAddActionPerformed(ActionEvent e) throws SQLException {
		int stdNum = Integer.parseInt(pStdNum.getTf().getText().trim());
		String stdName = pStdName.getTf().getText().trim();
		if (dao.searchItem(new Student(stdNum)) == null) {
			dao.insertItem(new Student(stdNum, stdName));
			pKor.getTf().setEditable(true);
			pEng.getTf().setEditable(true);
			pMath.getTf().setEditable(true);

		} else {
			int kor = Integer.parseInt(pKor.getTf().getText().trim());
			int eng = Integer.parseInt(pEng.getTf().getText().trim());
			int math = Integer.parseInt(pMath.getTf().getText().trim());
			dao.updateItem(new Student(stdNum, stdName, kor, eng, math));
		}
		clearTf();
	}

	protected void btnPrnActionPerformed(ActionEvent e) {
		prnPanel.setVisible(true);
	}

	protected void btnAllDelActionPerformed(ActionEvent e) throws SQLException {
		dao.deleteAllItem();
	}

	private void clearTf() {
		pStdNum.setTf("");
		pStdName.setTf("");
		pKor.setTf("");
		pEng.setTf("");
		pMath.setTf("");
		pStdName.getTf().setEditable(true);
	}
}
